package com.meilai.project.controller.web.finance.paymentReceived;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.meilai.project.dto.base.ApiResult;
import com.meilai.project.entity.finance.paymentReceived.AccountReceived;
import com.meilai.project.service.finance.paymentReceived.AccountReceivedService;
import com.meilai.project.vo.finance.paymentReceived.AccountReceivedVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 到款表 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2022-12-02
 */
@RestController
@RequestMapping("api/web/account_received")
public class AccountReceivedController {

    @Autowired
    private AccountReceivedService accountReceivedService;

    @GetMapping("/page")
    @ApiOperation("列表分页")
    public ApiResult<Page<AccountReceivedVO>> page(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                   @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                                                   @RequestParam(name = "begin_time", required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin_time,
                                                   @RequestParam(name = "end_time", required = false)
                                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end_time,
                                                   @RequestParam(name = "customer_id", required = false) Long customer_id) {
        return ApiResult.ok(accountReceivedService.page(page, page_size, begin_time, end_time, customer_id));
    }

    @GetMapping("/salePage")
    @ApiOperation("销售查看付款")
    public ApiResult<Page<AccountReceivedVO>> salePage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                                                       @RequestParam(name = "begin_time", required = false)
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                       @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime begin_time,
                                                       @RequestParam(name = "end_time", required = false)
                                                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
                                                       @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end_time,
                                                       @RequestParam(name = "customer_id", required = false) Long customer_id) {
        return ApiResult.ok(accountReceivedService.salePage(page, page_size, begin_time, end_time, customer_id));
    }

    @GetMapping("/countUnHandled")
    @ApiOperation("销售查看需要处理的付款个数")
    public ApiResult<Integer> countUnHandled() {
        return ApiResult.ok(accountReceivedService.countUnHandled());
    }


    @PostMapping("/save")
    @ApiOperation("保存到款数据")
    public ApiResult<Boolean> save(@RequestBody AccountReceived accountReceived) {
        accountReceived.setConfirm(false);
        return ApiResult.ok(accountReceivedService.save(accountReceived));
    }

    @PostMapping("/edit")
    @ApiOperation("修改到款数据")
    public ApiResult<Boolean> edit(@RequestBody AccountReceived accountReceived) {
        return ApiResult.ok(accountReceivedService.updateById(accountReceived));
    }

    @DeleteMapping("/remove")
    @ApiOperation("删除到款数据")
    public ApiResult<Boolean> remove(@RequestParam("id") Long id) {
        return ApiResult.ok(accountReceivedService.update(Wrappers.<AccountReceived>lambdaUpdate().set(AccountReceived::getDeleted, true).eq(AccountReceived::getId, id)));
    }

    @PostMapping("/confirmBatch")
    @ApiOperation("批量确认数据无误")
    public ApiResult<Boolean> confirm(@RequestBody List<Long> id) {
        return ApiResult.ok(accountReceivedService.update(Wrappers.<AccountReceived>lambdaUpdate().set(AccountReceived::getConfirm, true).in(AccountReceived::getId, id)));
    }

    @PostMapping("/list")
    @ApiOperation("列表分页")
    public ApiResult<List<AccountReceivedVO>> queryListByIds(@RequestBody List<Long> ids) {
        return ApiResult.ok(accountReceivedService.queryListByIds(ids));
    }
}
